package com.qupai.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.constant.MessageConstant;
import com.qupai.constant.StatusConstant;
import com.qupai.dto.PtPrizesDTO;
import com.qupai.dto.PtPrizesPageQueryDTO;
import com.qupai.entity.PtPrize;
import com.qupai.exception.DeletionNotAllowedException;
import com.qupai.mapper.PtPrizeMapper;
import com.qupai.result.PageResult;
import com.qupai.service.PtPrizesService;
import com.qupai.vo.PtPrizesVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class PtPrizesServiceImpl extends ServiceImpl<PtPrizeMapper, PtPrize> implements PtPrizesService {

    private final PtPrizeMapper volunteerPrizesMapper;

    /**
     * 新增奖品
     */
    @Override
    @Transactional//事务注解，要么全成功，要么全失败,在启动类已经开启事务注解
    public void save(PtPrizesDTO prizesDTO) {
        PtPrize prizes=new PtPrize();
        BeanUtils.copyProperties(prizesDTO,prizes);

        Long number= 0L;
        //设置默认被收藏数量
        prizes.setFavoritesCount(number);

        //插入一条数据
        volunteerPrizesMapper.insert(prizes);

    }

    /**
     * 奖品分页查询
     */
    @Override
    public PageResult pageQuery(PtPrizesPageQueryDTO prizesPageQueryDTO) {
        Page<PtPrize> page=prizesPageQueryDTO.toMpPageDefaultSortByUpdateTimeDesc();

        Page<PtPrize>p=lambdaQuery()
                .like(prizesPageQueryDTO.getName()!=null, PtPrize::getName,prizesPageQueryDTO.getName())
                .eq(prizesPageQueryDTO.getOrganizationsId()!=null, PtPrize::getOrganizationsId,prizesPageQueryDTO.getOrganizationsId())
                .eq(prizesPageQueryDTO.getStatus()!=null, PtPrize::getStatus,prizesPageQueryDTO.getStatus())
                .page(page);

        return new PageResult(p.getTotal(),p.getPages(),p.getRecords());
    }


    /**
     * 按照ids批量删除奖品
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            PtPrize prizes=volunteerPrizesMapper.selectById(id);
            //起售中不能删除
            if (prizes.getStatus().equals(StatusConstant.ENABLE) ){
                throw new DeletionNotAllowedException(MessageConstant.PRIZES_ON_SALE);
            }
        }
        volunteerPrizesMapper.deleteBatchIds(ids);

    }

    /**
     * 根据id查询奖品
     */
    @Override
    public PtPrizesVO getById(Long id) {

        PtPrize prizes=volunteerPrizesMapper.selectById(id);
        PtPrizesVO prizesVO=new PtPrizesVO();
        //将查询到的奖品信息复制到VO对象中
        BeanUtils.copyProperties(prizes,prizesVO);
        return prizesVO;
    }

    /**
     * 修改菜品
     */
    @Override
    public void update(PtPrizesDTO prizesDTO) {
        PtPrize prizes=new PtPrize();
        BeanUtils.copyProperties(prizesDTO,prizes);

        volunteerPrizesMapper.updateById(prizes);
    }

    /**
     * 根据id启用或禁用奖品的状态
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        PtPrize prizes = PtPrize.builder()
                .id(id)
                .status(status)
                .build();

        volunteerPrizesMapper.updateById(prizes);
    }

    /**
     * 根据部门分类id查询奖品
     */
    @Override
    public List<PtPrize> list(Long organizationsId) {
        PtPrize prizes = PtPrize.builder()
                .status(StatusConstant.ENABLE)
                .organizationsId(organizationsId)
                .build();
        return volunteerPrizesMapper.list(prizes);
    }

    /**
     * 根据学院分类id查询奖品
     */
    @Override
    public List<PtPrizesVO> selectPrizesList(PtPrize prizes) {
        List<PtPrize>PrizesList=volunteerPrizesMapper.list(prizes);
        List<PtPrizesVO> prizesVOList=new ArrayList<>();
        for (PtPrize p:PrizesList){
            PtPrizesVO prizesVO=new PtPrizesVO();
            BeanUtils.copyProperties(p,prizesVO);
            prizesVOList.add(prizesVO);
        }
        return prizesVOList;
    }
}
